﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GestionRecaudos
{
    public partial class FrmShowTotalVendido : Form
    {
        public bool BloquearFechas = true;

        public FrmShowTotalVendido()
        {
            InitializeComponent();

            this.FechaIniDateTimePicker.Value = DateTime.Now;
            this.FechaFinDateTimePicker.Value = DateTime.Now;
        }

        private void FechaIniDateTimePicker_Leave(object sender, EventArgs e)
        {
            this.FechaFinDateTimePicker.Value = this.FechaIniDateTimePicker.Value;
        }

        private void FrmShowTotalVendido_Load(object sender, EventArgs e)
        {
            this.FechaIniDateTimePicker.Enabled = !this.BloquearFechas;
            this.FechaFinDateTimePicker.Enabled = !this.BloquearFechas;
            this.actualizarButton_Click(null, null);
        }

        private void actualizarButton_Click(object sender, EventArgs e)
        {
            try
            {
                string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[Utils.get().getValorParametro("SeccionAppConfigGestionRecaudosConnectionString")].ConnectionString;
                DataTable table = new DataTable();
                table.TableName = this.tittle.Text;
                using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectionString))
                {
                    conn.Open();
                    System.Data.SqlClient.SqlCommand command = conn.CreateCommand();
                    command.CommandType = CommandType.StoredProcedure;
                    command.CommandText = "[spRecaudo]";
                    command.Parameters.Add(new System.Data.SqlClient.SqlParameter("@OP", "ObtenerTotalVendidoHoy"));
                    command.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ParamAuxiliar", this.FechaIniDateTimePicker.Text));
                    command.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Observaciones", this.FechaFinDateTimePicker.Text));
                    table.Load(command.ExecuteReader());
                    conn.Close();
                }

                if (table.Rows.Count > 0)
                {
                    this.totalRecaudosTextBox.Text = table.Rows[0]["SubTotalRecaudos"].ToString();
                    this.totalDevolucionTextBox.Text = table.Rows[0]["SubTotalDevoluciones"].ToString();
                    this.totalTextBox.Text = table.Rows[0]["Total"].ToString();
                }
            }
            catch (Exception ex)
            {
                MessageException.show(ex);
                return;
            }
        }
    }
}
